<template>
  <div class="table">
    <dv-border-box-11 :color="['#11caff']" backgroundColor="transparent" title="权限信息">
    <el-table
      :data="tableData"
      class="customer-table"
      :header-cell-style="headClass"
      row-key="id"
      height="83vh"
      default-expand-all
      :tree-props="{ children: 'children' }"
    >
      >
      <el-table-column type="index" width="50" align="left"> </el-table-column>
      <el-table-column prop="resName" label="路由名称" align="left">
      </el-table-column>
      <el-table-column prop="resPath" label="路由路径" align="left">
      </el-table-column>
      <el-table-column label="类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 'menu'">菜单</span>
          <span v-if="scope.row.type == 'nav'">导航</span>
          <span v-if="scope.row.type == 'botton'">按钮</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 'Y'">启用</span>
          <span v-if="scope.row.status == 'N'">禁用</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime.substring(0, 10) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="change(scope.row)"
            >编辑</el-button
          >
          <el-button
            v-if="scope.row.status == 'N'"
            size="mini"
            type="primary"
            @click="start(scope.row)"
            >启用</el-button
          >
          <el-button
            v-if="scope.row.status == 'Y'"
            size="mini"
            type="danger"
            @click="stop(scope.row)"
            >禁用</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    </dv-border-box-11>

    <!-- 路由编辑 -->
    <el-dialog
      class="power-update"
      :title="title"
      :inline="true"
      :visible.sync="dialogFormVisible"
      :append-to-body="true"
    >
      <el-form
        class="clearfix"
        :model="powerform"
        ref="powerruleform"
        label-position="left"
        label-width="100px"
        :rules="powerrules"
      >
        <el-form-item label="路由名称" prop="resName">
          <el-input v-model="powerform.resName"></el-input>
        </el-form-item>
        <el-form-item label="路由路径" prop="resPath">
          <el-input v-model="powerform.resPath"></el-input>
        </el-form-item>
        <el-form-item label="路由描述" prop="resDesc">
          <el-input v-model="powerform.resDesc"></el-input>
        </el-form-item>
        <el-form-item label="路由标识" prop="resKey">
          <el-input v-model="powerform.resKey"></el-input>
        </el-form-item>
        <el-form-item label="路由类型" prop="type">
          <el-input v-model="powerform.type"></el-input>
        </el-form-item>
        <el-form-item label="路由图标" prop="type">
          <el-input v-model="powerform.icon"></el-input>
        </el-form-item>
        <el-form-item label="父级路由" prop="parentId">
          <el-select
            v-model="powerform.parentName"
            placeholder="请选择父级路由"
            @change="handleChange($event)"
          >
            <el-option
              v-for="(item, index) in this.tableData"
              :label="item.resName"
              :key="item.index"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="changepower(powerform)">修改</el-button>
          <el-button @click="changeout">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      title: "",
      powerform: {
        id: "",
        icon: "",
        parentId: "",
        resName: "",
        resPath: "",
        resDesc: "",
        resKey: "",
        status: "",
        type: "",
        parentName: "",
      },
      powerruleform: {
        parentId: "",
        resName: "",
        resPath: "",
        resDesc: "",
        resKey: "",
      },
      powerrules: {
        parentId: [
          { required: true, message: "父级路由不能为空", trigger: "blur" },
        ],
        resName: [
          { required: true, message: "路由名称不能为空", trigger: "blur" },
        ],
        resPath: [
          { required: true, message: "路由路径不能为空", trigger: "change" },
        ],
        resDesc: [
          { required: true, message: "路由描述不能为空", trigger: "blur" },
        ],
        resKey: [
          { required: true, message: "标识符不能为空", trigger: "blur" },
        ],
      },
      dialogFormVisible: false,
    };
  },
  methods: {
    headClass() {
      return "background-color: transparent;color:#fff;font-weight:bold;";
    },
    // 获取权限列表
    getpower() {
      this.$http
        .get("/router/list")
        .then((res) => {
          if (res.data.h.code == 0) {
            let oneArr = [],
              anotherArr = [];
            oneArr = res.data.b.filter((item) => !item.parentId); //一级路由
            anotherArr = res.data.b.filter((item) => item.parentId); //二级路由
            oneArr.map((obj) => {
              obj.children = [];
            });
            anotherArr.map((item) => {
              oneArr.map((obj) => {
                if (item.parentId == obj.id) {
                  obj.children.push(item);
                }
              });
            });
            this.tableData = oneArr;
          }
        })
        .catch((err) => {});
    },
    change(row) {
      this.title = row.resName;
      this.dialogFormVisible = true;
      this.powerform = Object.assign(this.powerform, row);
    },
    handleChange($event) {
      this.powerform.parentName = $event.resName;
      this.powerform.parentId = $event.parentId;
    },
    changepower(form) {
      let params = {
        id: form.id,
        icon: form.icon,
        parentId: form.parentId,
        resDesc: form.resDesc,
        resKey: form.resKey,
        resName: form.resName,
        resPath: form.resPath,
        status: form.status,
        type: form.type,
      };
      this.$refs.powerruleform.validate((valid) => {
        if (valid) {
          this.$http
            .put("/router/update", params)
            .then((res) => {
              if (res.data.h.code == 0) {
                this.changeout();
                this.getpower();
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
              }
            })
            .catch((err) => {});
        } else {
          this.$message({
            message: "表单填写有误，请重新填写",
            type: "warning",
          });
          return false;
        }
      });
    },
    changeout() {
      this.dialogFormVisible = false;
      this.$refs.powerruleform.clearValidate();
      this.powerformclear();
    },
    powerformclear() {
      for (let i in this.powerform) {
        this.powerform[i] = "";
      }
    },
    start(row) {
      this.$http
        .put("/router/enable/" + row.id)
        .then((res) => {
          if (res.data.h.code == 0) {
            this.$message({
              message: "启用成功",
              type: "success",
            });
            this.getpower();
          }
        })
        .catch((err) => {});
    },
    stop(row) {
      this.$http
        .put("/router/disable/" + row.id)
        .then((res) => {
          if (res.data.h.code == 0) {
            this.$message({
              message: "禁用成功",
              type: "success",
            });
            this.getpower();
          }
        })
        .catch((err) => {});
    },
  },
  mounted() {
    this.getpower();
  },
};
</script>
<style scoped>
.power-update .el-form .el-form-item {
  display: inline-block;
  width: 45%;
}
.power-update .el-form .el-form-item {
  display: inline-block;
  width: 45%;
}
.power-update .el-form .el-form-item:nth-of-type(2n + 1) {
  margin-right: 2%;
}
.power-update .el-form .el-form-item .el-select {
  display: block;
}
.table{
  height:90vh;
}
.table >>> .el-table__expand-icon {
  color: #fff;
}
</style>
<style>
.customer-table th {
  border: none;
}
.customer-table td,
.customer-table th.is-leaf {
  border: none;
}
</style>